// import ResManager from "../GameAb/scripts/ResManager";
import ResLoading from "./ResLoading";
import ResLoadingAdapter from "./ResLoadingAdapter";
import Util from "./Util";
import WXAD from "./WXAd";
import WXRealLogUtil from "./WXRealLogUtil";
import WXUtil from "./WXUtil";

const { ccclass, property, executionOrder } = cc._decorator;
//@ts-ignore
const i18n = require('LanguageData');

@ccclass
@executionOrder(-1)
export default class Loading extends cc.Component {

    @property(cc.Label)
    versionLabel: cc.Label = null;

    protected onLoad(): void {
        window['changeLang'] = Util.changeLanuage
        i18n.init(Util.getLanguageStr())
        WXAD.instance.load()
    }

    protected start(): void {
        this._loadAndGoToNext()
    }

    _showRetry() {
        let self = this
        window['wx']?.showModal({
            title: '提示',
            content: '资源加载失败,请重试',
            confirmText: "确定",
            showCancel: false,
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    self._loadAndGoToNext()
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    }

    _loadAndGoToNext() {
        // WXRealLogUtil.info("开始加载首页");
        ResLoadingAdapter.show({
            title: '加载首页'
        })
        cc.assetManager.loadBundle("GameAb", {}, (err: Error, b: cc.AssetManager.Bundle) => {
            ResLoadingAdapter.hide()
            if (err) {
                WXUtil.retryCall(this._loadAndGoToNext, this)
                WXRealLogUtil.error("加载首页出错:" + err.message);
                return console.error(err.message);
            }
            WXRealLogUtil.info("加载首页完成");
            this.goToNext()
        })
    }

    goToNext() {
        Util.changeScene("Home")
    }
}
